/**
 * 69. x 的平方根
 */
class Solution5 {
    public int mySqrt(int x) {
        if(x == 0) return 0;

        int left = 1, right = x;

        while(left < right) {
            int mid = left + (right - left + 1) / 2;
            if(mid > x / mid) {
                // mid * mid > x
                right = mid - 1;
            } else {
                // mid * mid <= x
                left = mid;
            }
        }

        return left;
    }
}